package com.ruoyi.easyExcelHandler.converter;

import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.converters.ReadConverterContext;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.metadata.property.ExcelContentProperty;
import com.ruoyi.common.utils.DateUtils;


public class DateStrHanlderConverter implements Converter<String> {


    /**
     * 开启对 Character 类型的支持
     */
    @Override
    public Class<?> supportJavaTypeKey() {
        return String.class;
    }

    @Override
    public CellDataTypeEnum supportExcelTypeKey() {
        return CellDataTypeEnum.STRING;
    }

    @Override
    public String convertToJavaData(ReadConverterContext<?> context) {

        String val = null;
        if (context.getReadCellData().getStringValue() != null && context.getReadCellData().getStringValue() != "") {

            val = DateUtils.parseDateToStr("yyyy/MM/dd", DateUtils.parseDate(context.getReadCellData().getStringValue()));
        }
        return val;

    }

    /**
     * 自定义对 Character 类型数据的处理
     * 我这里就拿 String 去包装了下
     */
    @Override
    public WriteCellData<?> convertToExcelData(String value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) throws Exception {

        String val = null;
        if (value != null && value != "") {

            val = DateUtils.parseDateToStr("yyyy/MM/dd", DateUtils.parseDate(value));
        }
        return new WriteCellData<String>(val);
    }
}
